package com.japhet.stripesdemo.business.stripe;

import com.stripe.exception.StripeException;
import com.stripe.model.Customer;
import com.stripe.model.PaymentMethod;
import com.stripe.model.PaymentMethodCollection;
import com.stripe.param.CustomerCreateParams;
import com.stripe.param.PaymentMethodListParams;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class StripeServiceImpl implements StripeService {
    @Override
    public Customer create(String name, String email) throws StripeException {
        CustomerCreateParams params =
                CustomerCreateParams.builder()
                        .setName(name)
                        .setEmail(email)

                        .build();
        return Customer.create(params);
    }

    @Override
    public List<PaymentMethod> customerCardList(String customerId) throws StripeException {
        // 使用 PaymentMethod 列表端点获取客户的支付方式列表
        PaymentMethodListParams params = PaymentMethodListParams.builder()
                .setCustomer(customerId)
                .setType(PaymentMethodListParams.Type.CARD)  // 指定类型为银行卡
                .build();

        PaymentMethodCollection paymentMethods = PaymentMethod.list(params);
        if (CollectionUtils.isEmpty(paymentMethods.getData())) {
            return new ArrayList<>();
        }

        return paymentMethods.getData();
    }
}
